package com.zlx.sharelive.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.CountBean;
import com.zlx.module_base.base_api.res_data.UserLevelVoBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.activity.CompanionAc;
import com.zlx.sharelive.activity.ExtensionAc;
import com.zlx.sharelive.activity.FeedbackAc;
import com.zlx.sharelive.activity.GoddessAc;
import com.zlx.sharelive.activity.GoddessCertifiedAc;
import com.zlx.sharelive.activity.GoddessUnderCertificationAc;
import com.zlx.sharelive.activity.GradeAc;
import com.zlx.sharelive.activity.HomepageAc;
import com.zlx.sharelive.activity.MemberAc;
import com.zlx.sharelive.activity.MyFansAc;
import com.zlx.sharelive.activity.MyFollowAc;
import com.zlx.sharelive.activity.MyInfoAc;
import com.zlx.sharelive.activity.MyLikeAc;
import com.zlx.sharelive.activity.MyUploadAc;
import com.zlx.sharelive.activity.MyWalletAc;
import com.zlx.sharelive.activity.PurchasedVideoAc;
import com.zlx.sharelive.activity.SettingAc;
import com.zlx.sharelive.activity.ViewingVideoAc;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.NullTokenUtils;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的模块
 */
public class MyFg extends BaseFg {

    @BindView(R.id.iv_my_head)
    ImageView iv_my_head;
    @BindView(R.id.tv_my_nick)
    TextView tv_my_nick;
    @BindView(R.id.tv_my_id)
    TextView tv_my_id;
    @BindView(R.id.tv_my_content)
    TextView tv_my_content;
    @BindView(R.id.tv_like)
    TextView tv_like;
    @BindView(R.id.tv_follow)
    TextView tv_follow;
    @BindView(R.id.tv_fans)
    TextView tv_fans;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.iv_authentication)
    ImageView iv_authentication;
    @BindView(R.id.tv_buy)
    TextView tv_buy;

    private String authentication;
    private BaseInfoBean mBaseInfoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.fg_my;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getBaseInfo();
        getCount();
    }

    @Override
    public void onStart() {
        super.onStart();
        getBaseInfo();
        getCount();
        getUserLevelVo();
    }

    private void getUserLevelVo() {
        ApiUtil.getUserApi().getUserLevelVo(SPUtil.getToken(getContext())).observe(getViewLifecycleOwner(), new BaseObserver<>(new BaseObserverCallBack<ApiResponse<UserLevelVoBean>>() {
            @Override
            public void onSuccess(ApiResponse<UserLevelVoBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                if (data.getData().getInfo().size() > 0) {
//                                level = data.getData().getInfo().get(0).getLevel();
//                                score = data.getData().getInfo().get(0).getScore();
                                }
                            }
                        } else {
                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                        }
                    }
                }
            }
        }));
    }


    /**
     * 获取关注数量 粉丝数量 点赞数量
     */
    private void getCount() {
        ApiUtil.getUserApi().getCount(SPUtil.getToken(getContext())).observe(
                this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<CountBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<CountBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    setCount(data.getData());
                                } else {
                                    NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                                }
                            }
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    /**
     * 点赞 关注 粉丝
     *
     * @param data
     */
    private void setCount(CountBean data) {
        if (data.getInfo().size() > 0) {
            tv_like.setText(data.getInfo().get(0).getLike() + "");
            tv_follow.setText(data.getInfo().get(0).getFocusedCount() + "");
            tv_fans.setText(data.getInfo().get(0).getFansCount() + "");
        }
    }

    /**
     * 用户基本信息
     */
    private void getBaseInfo() {
        ApiUtil.getUserApi().getBaseInfo(SPUtil.getToken(getContext())).
                observe(getViewLifecycleOwner(), new BaseObserver<>(
                        new BaseObserverCallBack<ApiResponse<BaseInfoBean>>() {
                            @Override
                            public void onSuccess(ApiResponse<BaseInfoBean> data) {
                                if (null != data) {
                                    if (null != data.getData()) {
                                        if (data.getData().getCode() == 0) {
                                            setInfo(data.getData());
                                        } else {
                                            NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                                        }
                                    }
                                }

                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();

                            }
                        }));
    }

    /**
     * 设置个人资料
     *
     * @param data
     */
    private void setInfo(BaseInfoBean data) {
        mBaseInfoBean = data;
        if (data.getInfo().size() > 0) {
            GlideImgManager.glideLoader(getContext(), data.getInfo().get(0).getAvatar(), iv_my_head, 0);
            tv_my_nick.setText(data.getInfo().get(0).getNickname());
            tv_my_id.setText("ID:" + data.getInfo().get(0).getId());
            tv_my_content.setText(data.getInfo().get(0).getSignature());
            if (data.getInfo().get(0).getIsvip().equals("1")) {
                iv_vip.setVisibility(View.VISIBLE);
                tv_buy.setVisibility(View.GONE);
            } else {
                iv_vip.setVisibility(View.GONE);
                tv_buy.setVisibility(View.VISIBLE);
            }
            if (!StringUt.isEmpty(data.getInfo().get(0).getAuthentication())) {
                authentication = data.getInfo().get(0).getAuthentication();
                if (data.getInfo().get(0).getAuthentication().equals("1")) {
                    iv_authentication.setVisibility(View.VISIBLE);
                } else {
                    iv_authentication.setVisibility(View.GONE);
                }
            }

        }
    }


    @OnClick({R.id.iv_setting, R.id.ll_like, R.id.ll_follow, R.id.ll_fans,
            R.id.rl_wallet, R.id.iv_my_head, R.id.ll_goddess, R.id.rl_extension
            , R.id.rl_my_upload, R.id.ll_purchased, R.id.ll_viewing, R.id.ll_companion
            , R.id.ll_grade, R.id.ll_homepage, R.id.tv_buy, R.id.rl_member, R.id.rl_feedback})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.rl_feedback:
                //意见反馈
                startActivity(new Intent(getContext(),
                        FeedbackAc.class));
                break;
            case R.id.rl_member:
            case R.id.tv_buy:
                //会员
                startActivity(new Intent(getContext(), MemberAc.class)
                        .putExtra("data", mBaseInfoBean));
                break;
            //设置
            case R.id.iv_setting:
                startActivity(new Intent(getContext(), SettingAc.class));
                break;
            //点赞
            case R.id.ll_like:
                startActivity(new Intent(getContext(), MyLikeAc.class));
                break;
            //关注
            case R.id.ll_follow:
                startActivity(new Intent(getContext(), MyFollowAc.class));
                break;
            //粉丝
            case R.id.ll_fans:
                startActivity(new Intent(getContext(), MyFansAc.class));
                break;
            //钱包
            case R.id.rl_wallet:
                startActivity(new Intent(getContext(), MyWalletAc.class));
                break;
            //个人资料
            case R.id.iv_my_head:
                if (null != mBaseInfoBean) {
                    startActivity(new Intent(getContext(), MyInfoAc.class).putExtra("info", mBaseInfoBean));
                }
                break;
            //女神认证
            case R.id.ll_goddess:
                if (null != mBaseInfoBean) {
                    if (null != mBaseInfoBean.getInfo()) {
                        if (mBaseInfoBean.getInfo().size() > 0) {
                            if (mBaseInfoBean.getInfo().get(0).getSex().equals("2")) {
                                switch (authentication) {
                                    case "0":
                                        //认证中
                                        startActivity(new Intent(getContext(), GoddessUnderCertificationAc.class));
                                        break;
                                    case "1":
                                        //已认证
                                        startActivity(new Intent(getContext(), GoddessCertifiedAc.class));
                                        break;
                                    case "2":
                                    default:
                                        //未认证
                                        startActivity(new Intent(getContext(), GoddessAc.class));
                                        break;
                                }
                            } else {
                                Toast.makeText(getContext(), "只有女性用户可以认证", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                }
                break;
            //推广
            case R.id.rl_extension:
                startActivity(new Intent(getContext(), ExtensionAc.class));
                break;
            //我的上传
            case R.id.rl_my_upload:
                startActivity(new Intent(getContext(), MyUploadAc.class));
                break;
            //已购影片
            case R.id.ll_purchased:
                startActivity(new Intent(getContext(), PurchasedVideoAc.class));
                break;
            //观影记录
            case R.id.ll_viewing:
                startActivity(new Intent(getContext(), ViewingVideoAc.class));
                break;
            //伴侣
            case R.id.ll_companion:
                startActivity(new Intent(getContext(), CompanionAc.class));
                break;
            //等级
            case R.id.ll_grade:
                startActivity(new Intent(getContext(), GradeAc.class)
                        .putExtra("info", mBaseInfoBean));
                break;
            //个人主页
            case R.id.ll_homepage:
                startActivity(new Intent(getContext(), HomepageAc.class));
                break;
        }
    }
}
